import  tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

# 载入数据集
mnist =input_data.read_data_sets('MNIST_data',one_hot=True)
# 定义每个批次的大小,类似于缓冲器
batch_size =80
# 计算一共多少批次  //整除
n_batch = mnist.train.num_examples // batch_size
# 定义PLACEHOLDER
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
rate = tf.placeholder(tf.float32)
lr = tf.Variable(0.001,dtype=tf.float32)

# 创建简单的神经网络
# Weights = tf.Variable(tf.zeros([784,10]))
# biases = tf.Variable(tf.zeros([10]))
# stddev：正太分布的标准差
Weights1 = tf.Variable(tf.truncated_normal([784,200],stddev=0.1))
biases1 = tf.Variable(tf.zeros([200])+0.1)
# keep_prob= 百分之多少神经元正常工作 解决过拟合
L1 = tf.nn.tanh(tf.matmul(x,Weights1)+biases1)
# rate = 1- keep_prob
L1_drop = tf.nn.dropout(L1,rate=0.25)

# 开始增加隐藏层
Weights2 = tf.Variable(tf.truncated_normal([200,200],stddev=0.1))
biases2 = tf.Variable(tf.zeros([200])+0.1)
L2 = tf.nn.tanh(tf.matmul(L1_drop,Weights2)+biases2)
L2_drop = tf.nn.dropout(L2,rate=0.25)

Weights3 = tf.Variable(tf.truncated_normal([200,100],stddev=0.1))
biases3 = tf.Variable(tf.zeros([100])+0.1)
L3 = tf.nn.tanh(tf.matmul(L2_drop,Weights3)+biases3)
L3_drop = tf.nn.dropout(L3,rate=0.25)

Weights4 = tf.Variable(tf.truncated_normal([100,10],stddev=0.1))
biases4 = tf.Variable(tf.zeros([10])+0.1)

# 下面的那个交叉熵里面好像会求softmax
# prediction = tf.nn.softmax(tf.matmul(L3_drop,Weights4)+biases4)
prediction = tf.matmul(L3_drop,Weights4)+biases4

# 二次代价 分别为 均方误差最小和交叉熵平均最小
# loss = tf.reduce_mean(tf.square(y-prediction))
loss= tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))

# 梯度下降
# train_step = tf.train.GradientDescentOptimizer(0.4).minimize(loss)
# 对于我的 反而没有梯度下降来的准确
train_step = tf.train.AdamOptimizer(lr).minimize(loss)

# 初始化变量
init = tf.global_variables_initializer()

#  argmax() 返回预测值最大的是那个位置
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
# 准确率的方法  由布尔类型转化为浮点32  求均值  1.0和0
accuracy  = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    sess.run(init)
    # 训练21次
    for epoch in range(50):
        # 训练所有的图
        # 每次迭代 改变学习率 越靠近越慢
        sess.run(tf.assign(lr,0.001*(0.95**epoch)))
        for batch in range(n_batch):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        test_acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
        train_acc = sess.run(accuracy, feed_dict={x: mnist.train.images, y: mnist.train.labels})
        learning_rate = sess.run(lr)
        print('Iter'+str(epoch)+',Testing Accuracy:'+str(test_acc)+',Training Accuracy:'+str(train_acc)+'Learning rate:'+str(learning_rate))

# 改进  目标从91%到95%   目前93%   97.5%
#         1. 缓冲读取   ok
#         2.再增加隐藏层
#         3.初始化不为零
#         4.二次代价改为交叉熵？
#         5.梯度下降的学习率，或其他的方式    ok
#         6.21次训练次数增加，考虑收敛    ok

